package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.widget.RadioGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.UserDao;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.CorrectRespEvent;
import com.hangzhou.xyj.model.type.ECorrectType;
import com.hangzhou.xyj.net.handler.UpdateSexHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.UpdateSexReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView.OnNavRightListener;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.ToastUtil;

public class MyCorrectGenderActivity extends BaseActivity {
    RadioGroup rgGender;
    Intent intent;
    public static final int SUCCESS = 1;
    private String mCurrentValue;

    @Override
    public void initTop() {
        enableTitle(true, "性别");
        enableRightNav(true, "确定");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_correct_gender);
    }

    @Override
    public void initViews() {
        rgGender = (RadioGroup) findViewById(R.id.rg_gender);
        intent = getIntent();
    }

    @Override
    public void initDatas() {
        if ("女".equals(intent.getStringExtra("value"))) {
            mCurrentValue = "女";
            rgGender.check(R.id.rb_female);
        } else {
            mCurrentValue = "男";
            rgGender.check(R.id.rb_male);
        }
    }

    @Override
    public void initListeners() {
        setOnNavRightListener(new OnNavRightListener() {
            int userId = Integer.valueOf(CommonPreferences.getUserId(mContext));

            @Override
            public void onNavRight() {
                if (rgGender.getCheckedRadioButtonId() == R.id.rb_male) {
                    intent.putExtra("value", "男");
                    mCurrentValue = "男";
                    updateGender(userId, "男");
                } else {
                    intent.putExtra("value", "女");
                    mCurrentValue = "女";
                    updateGender(userId, "女");
                }
            }
        });
    }

    /**
     * 更新性别到服务器
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/7 16:06
     */
    public void updateGender(int userId, String gender) {
        UpdateSexReq updateSexReq = new UpdateSexReq(userId, gender);
        HttpLoader.getInstance().sendRequest(new UpdateSexHandler(updateSexReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    //更新应用中的信息
                    XYJApplication.getUserInfo().setSex(mCurrentValue);
                    //更新本地数据库内容
                    UserDao dao = new UserDao(mContext);
                    dao.addUser(XYJApplication.getUserInfo());
                    //提示成功
                    ToastUtil.show("保存成功！");
                    CorrectRespEvent event = new CorrectRespEvent();
                    event.setTag(ECorrectType.GENDER.getValue());
                    event.setValue(mCurrentValue);
                    EventBusUtil.postEvent(event);
                    MyCorrectGenderActivity.this.setResult(SUCCESS, intent);// 1表示成功
                    MyCorrectGenderActivity.this.finish();// 关闭本窗口
                }
            }

        }, 0));
    }
}
